<template>
	<view>
		<view class="good-list">
			<view v-for="(item, index) in goodList" :key="index" @click="toDetail(item.goods_id)">
				<cus-good :goods="item"></cus-good>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				queryobj: {
					query: '',
					cid: '',
					pagenum: 1,
					pagesize: 10
				},
				// 商品列表
				goodList: [],
				total: 0,
				// 节流阀
				isLoading: false
			}
		},

		methods: {
			// 获取商品列表数据
			async getGoodLsit(cb) {
				// 打开节流阀
				this.isLoading = true
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/goods/search', this.queryobj)
				// 数据获取完毕，关闭下拉刷新
				cb && cb()
				// console.log(res);
				if (res.meta.status !== 200) return uni.$showMsg()
				this.goodList = [...this.goodList, ...res.message.goods]
				this.total = res.message.total
				// 关闭节流阀
				this.isLoading = false
			},

			// 跳转详情页
			toDetail(id) {
				// console.log(id);
				uni.navigateTo({
					url: '/subpkg/goods_detail/goods_detail?goods_id=' + id
				})
			}
		},

		onLoad(options) {
			// console.log(options);
			this.cid = options.cid || ''
			this.query = options.query || ''
			// 获取商品列表
			this.getGoodLsit()
		},

		// 下拉加载
		onReachBottom() {
			// 判断是都加载完毕
			if (this.queryobj.pagenum * this.queryobj.pagesize >= this.total) return uni.$showMsg('数据加载完毕')
			// 判断是否正在加载
			if (this.isLoading) return uni.$showMsg('数据加载中')
			this.queryobj.pagenum += 1
			this.getGoodLsit()
		},

		// 上拉刷新
		onPullDownRefresh() {
			// 重置数据
			this.queryobj.pagenum = 1
			this.total = 0
			this.goodList = []
			this.isLoading = false
			// 加载数据，加载完毕下拉刷新
			this.getGoodLsit(() => {
				uni.stopPullDownRefresh()
			})
		}
	}
</script>

<style lang="scss">

</style>
